package com.qf.service;

import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.util.CheckMobileOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Set;

/**
 * 实现号段补全过滤策略
 *
 *     "phase,black,limitFiveMinutes,dirtyWords,fee,router".split(,)
 * @author lixu
 */
@Service("phase")
public class PhaseFilterServiceImpl implements FilterChainService {

    @Autowired
    private CacheFeign cacheFeign;
    /**
     * 实现号段补全过滤策略
     * @param standardSubmit
     * @return
     */
    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        System.out.println("======实现号段补全过滤策略====");
        //**号段补全 : **从号段表中查询手机所在区域补全手机号段信息，号段信息可从REDIS缓存中取，找不到号段信息就填0，表示未知。
        //手机号
        String destMobile = standardSubmit.getDestMobile();
        //1:运营商
        if(CheckMobileOperator.isChinaMobilePhoneNum(destMobile)){
            //移动  类型
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);
        }else if(CheckMobileOperator.isChinaUnicomPhoneNum(destMobile)){
            //联通
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);
        }else if(CheckMobileOperator.isChinaTelecomPhoneNum(destMobile)){
            //电信  类型
            standardSubmit.setOperatorId(OperatorConstants.CHINA_TELECOM);
        }else{
            //未知  类型
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
        }

        String preMobile = destMobile.substring(0, 7);
        System.out.println("===手机号前7位:" + preMobile);
        //2: 省
        String preMobileKey = CacheConstants.CACHE_PREFIX_PHASE + preMobile;
        String value = cacheFeign.get(preMobileKey);
        if(StringUtils.isEmpty(value)){
            standardSubmit.setProvinceId(OperatorConstants.CHINA_UNKNOWN);
            standardSubmit.setCityId(OperatorConstants.CHINA_UNKNOWN);
        }else{
            //2: 省
            String[] p = value.split(",");
            standardSubmit.setProvinceId(Integer.parseInt(p[0]));
            //3: 市
            standardSubmit.setCityId(Integer.parseInt(p[1]));
        }
        //3:下发网关ID号   1   （通道ID）
        //条件： 2、通道类型（运营商类型） 13777777777  正则  中国电信：类型是 3
        //SQL : select id from t_channel where channelType = 3
        //redis:   key :  CHANNEL:中国电信类型:channelid
        String key = CacheConstants.CACHE_PREFIX_SMS_CHANNEL + standardSubmit.getOperatorId() + ":*";
        Set<String> setKeys = cacheFeign.keys(key);
        if(!CollectionUtils.isEmpty(setKeys) && setKeys.size() > 0){
            for (String setKey : setKeys) {
                //1) "CHANNEL:3:3"
                String[] keys = setKey.split(":");
                standardSubmit.setGatewayID(Integer.parseInt(keys[2]));
            }

        }
        //4: 下发的源号码 10698888888    1069777777   用户在运营商开辟的账户ID
        // 号段补全策略实现   需要使用spNumber（channel表） + extendNumber（client_channel表）
        String spnumberKey = CacheConstants.CACHE_PREFIX_SMS_CHANNEL
                //运营商类型  channelType
                + standardSubmit.getOperatorId() + ":" + standardSubmit.getGatewayID();//运营商ID(通道ID）
        String spnumber = cacheFeign.hget(spnumberKey, "spnumber");

        //ROUTER:客户ID:通道ID
        String extendNumberKey = CacheConstants.CACHE_PREFIX_ROUTER
                + standardSubmit.getClientID() + ":" + standardSubmit.getGatewayID();//通道ID
        Map map = cacheFeign.hMGet(extendNumberKey);
        String extendnumber = (String) map.get("extendnumber");
        standardSubmit.setSrcNumber(spnumber + extendnumber);

        //5:短信费用  单位是分
        String price = (String) map.get("price");
        standardSubmit.setFee(Long.parseLong(price));
        System.out.println(standardSubmit);
        //成功
        return true;
    }
}
